<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace app\adminapi\controller\v1\system;

use app\adminapi\controller\Auth;
use app\services\system\SystemMenusServices;
use app\services\system\SystemRoleServices;
use think\facade\App;

/**
 * Class：系统角色控制器
 * 创建人：Jack
 * 创建时间：2023/8/1 9:09
 * @package app\adminapi\controller\v1\system
 */
class SystemRole extends Auth
{
    /**
     * SystemRole constructor.
     * @param App                $app
     * @param SystemRoleServices $services
     */
    public function __construct(App $app, SystemRoleServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 注释：角色列表
     * 创建人：Jack
     * 创建时间：2023/8/1 9:16
     * @return mixed
     */
    public function index()
    {
        $where = $this->request->getMore([
            ['status', ''],
            ['role_name', ''],
        ]);
        $where['level'] = $this->adminInfo['level'] + 1;

        return $this->success($this->services->getRoleList($where));
    }

    /**
     * 注释：角色详情
     * 创建人：Jack
     * 创建时间：2023/4/10 8:30
     * @param SystemMenusServices $services
     * @param                     $id
     * @return mixed
     */
    public function read(SystemMenusServices $services, $id)
    {
        $role = $this->services->get($id);
        if (!$role) {
            return $this->fail('修改的角色不存在');
        }
        $menus = $services->getMenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles'], 0);
        return $this->success(['role' => $role->toArray(), 'menus' => $menus]);
    }

    /**
     * 注释：获取角色菜单
     * 创建人：Jack
     * 创建时间：2023/4/10 8:31
     * @param SystemMenusServices $services
     * @return mixed
     */
    public function role_menus(SystemMenusServices $services)
    {
        $menus = $services->getMenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles'], 0);
        return $this->success(compact('menus'));
    }

    /**
     * 注释：创建修改角色
     * 创建人：Jack
     * 创建时间：2023/4/10 8:24
     * @param $id
     * @return mixed
     */
    public function save($id)
    {
        $data = $this->request->postMore([
            'role_name',
            ['status', 0],
            ['checked_menus', [], '', 'rules']
        ]);
        if (!$data['role_name']) return $this->fail('请输入角色名称');
        if (!is_array($data['rules']) || !count($data['rules'])) {
            return $this->fail('请选择最少一个权限');
        }
        $data['rules'] = implode(',', $data['rules']);
        if ($id) {
            if (!$this->services->update($id, $data)) return $this->fail('修改失败!');
            \hdphp\services\CacheService::clear();
            return $this->success('修改成功!');
        } else {
            $data['level'] = $this->adminInfo['level'] + 1;
            if (!$this->services->save($data)) return $this->fail('添加角色失败!');
            \hdphp\services\CacheService::clear();
            return $this->success('添加角色成功!');
        }
    }

    /**
     * 注释：修改角色状态
     * 创建人：Jack
     * 创建时间：2023/8/1 9:17
     * @param $id
     * @param $status
     * @return mixed
     */
    public function set_status($id, $status)
    {
        if (!$id) {
            return $this->fail('缺少参数');
        }
        $role = $this->services->get($id);
        if (!$role) {
            return $this->fail('没有查到此身份');
        }
        $role->status = $status;
        if ($role->save()) {
            \hdphp\services\CacheService::clear();
            return $this->success('修改成功');
        } else {
            return $this->fail('修改失败');
        }
    }

    /**
     * 注释：删除角色
     * 创建人：Jack
     * 创建时间：2023/8/1 9:18
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        if (!$this->services->delete($id)) {
            return $this->fail('删除失败,请稍候再试!');
        }
        \hdphp\services\CacheService::clear();
        return $this->success('删除成功!');
    }

    /**
     * 注释：下拉框角色列表
     * 创建人：Jack
     * 创建时间：2023/8/1 9:16
     * @return mixed
     */
    public function select_list()
    {
        $where['status'] = 1;
        $where['level'] = $this->adminInfo['level'] + 1;
        return $this->success($this->services->getAllRoleList($where));
    }
}